package week03.resume;

import java.util.Arrays;

/**
 * Created by zhangxiaokun on 2018/2/6.
 */
public class TestResume {
    //  测试clone()
    public static void main(String[] args) {
        testClone();
        testSerialClone();    //通过系列化 深度复制
        testCloneByTrantoJson();
    }

    //通过转成json串，解析----实现深度复制
    private static void testCloneByTrantoJson() {
        System.out.println("\n*******************通过转json，解析*******************");
        Resume resume = new Resume("zhangsan", "zhangsan .....", 22,
                Arrays.asList(new String[]{new String("13478780007"), "13478780008", "13478780009"}));
        Resume resumeClone = resume.myClone2();
        compare(resume, resumeClone);
    }

    //重写clone()  构建实例 方式实现的 深度复制
    private static void testClone() {
        System.out.println("\n*******************通过手动复制组装*******************");
        Resume resume = new Resume("zhangsan", "zhangsan .....", 22,
                Arrays.asList(new String[]{new String("13478780007"), "13478780008", "13478780009"}));
        try {
            Resume resumeClone = (Resume) resume.clone();
            compare(resume, resumeClone);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    //测试 系列化 方式实现的 深度复制
    public static void testSerialClone() {
        System.out.println("\n*******************通过系列化*******************");
        Resume resume = new Resume("zhangsan", "zhangsan .....", 22,
                Arrays.asList(new String[]{"13478780007", "13478780008", "13478780009"}));
        Resume resumeClone = (Resume) resume.myClone();
        compare(resume, resumeClone);
    }

    //比较
    public static void compare(Resume resume, Resume resumeClone) {
        System.out.println("***************开始比较*******************");
        System.out.println(resume == resumeClone);
        System.out.println(resume.hashCode() + "\t" + resumeClone.hashCode());

        //比较属性
        System.out.println("name:" + (resume.getName() == resumeClone.getName()));
        System.out.println("Introduction:" + (resume.getIntroduction() == resumeClone.getIntroduction()));
        System.out.println("Age:" + (resume.getAge() == resumeClone.getAge()));

        //比较数组内层
        System.out.println("----遍历比较IphoneNumberList 每一元素----");
        System.out.println("IphoneNumberList:" + (resume.getIphoneNumberList() == resumeClone.getIphoneNumberList()));
        resumeClone.getIphoneNumberList().set(0,  resumeClone.getIphoneNumberList().get(0) + "");

        for (int i = 0; i < resume.getIphoneNumberList().size(); i++) {
            System.out.println("iphoneNumber" + i + ":" +
                    (resume.getIphoneNumberList().get(i) == resumeClone.getIphoneNumberList().get(i)));
        }
    }
}

